#!/bin/bash

# This script menu uses the installation startup of VNC service and the installation startup of graphical desktop.

# 让你真正看到我的样子ヾ(≧O≦)〃

# 定义颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
RESET='\033[0m'
source /.kali-config/language.conf

# 更新系统
echo -e "${YELLOW}[*] Updating system...${RESET}"
apt update

# 创建配置文件夹
mkdir -p ~/.kali-config

# 检查并清理冲突的锁文件
if [ -f /tmp/.X1-lock ] || [ -f /tmp/.X11-unix/X1 ]; then
    echo -e "${YELLOW}[*] Removing stale X server lock files...${RESET}"
    rm -f /tmp/.X1-lock
    rm -f /tmp/.X11-unix/X1
fi

# Set log file path
LOG_DIR="/.kali-config/log"
LOG_FILE="$LOG_DIR/$(date +'%Y-%m-%d').log"

# Create log directory
mkdir -p "$LOG_DIR"

# Define log function
log() {
    echo -e "$1" >> "$LOG_FILE"
    echo -e "$2" # Display content in terminal
}

# Is VNC installed
VNCINSTALLIF(){
VNCPACKAGE="tigervnc-standalone-server"
if dpkg -l | grep -q "^ii[ ]*$VNCPACKAGE"; then
    log "[*]tigervnc-standalone-server package has been installed."
    VNCINSTALL="true"
else
    log "[*]tigervnc-standalone-server package is not installed."
    VNCINSTALL="false"
fi
}

NOVNCINSTALLIF(){
NOVNCPACKAGE="novnc"
if dpkg -l | grep -q "^ii[ ]*$NOVNCPACKAGE"; then
    log "[*]tigervnc-standalone-server package has been installed."
    NOVNCINSTALL="true"
else
    log "[*]tigervnc-standalone-server package is not installed."
    NOVNCINSTALL="false"
fi
}

# system operation
desktop_operate(){
echo "Try to uninstall Firefox."
echo "find / -type f -name "*firefox*" -exec rm -f {} \; 2>/dev/null"
find / -type f -name "*firefox*" -exec rm -f {} \; 2>/dev/null
echo "Try to install the Chromium browser."
echo "eatmydata apt install -y chromium chromium-l10n"
apt update
eatmydata apt install -y chromium chromium-l10n
echo "xdg-settings set default-web-browser chromium.desktop"
xdg-settings set default-web-browser chromium.desktop
}

# Repair time to China Shanghai time
desktop_time_repair(){
echo "ZONE=Asia/Shanghai" >> /etc/sysconfig/clock
rm -f /etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
}

#Chinese menu
menu_cn(){
vnc-installation(){
if [ -e ~/.vnc/passwd ]; then
IFVNCPASSWD="已设置"
else
IFVNCPASSWD="未设置"
fi
if [ -e ~/.kali-config/kali-desktop ]; then
Desktop_environment=$(cat ~/.kali-config/kali-desktop)
else
Desktop_environment="还没有桌面环境"
fi
echo -e """
\033[1;33m
+--------------+---------------+
  VNC 服务         是否安装      
+--------------+---------------+
 VNC server         ${VNCINSTALL}      
 NOVNC              ${NOVNCINSTALL}      
+--------------+---------------+

+-------------------------+
已安装的桌面环境       
+-------------------------+
${Desktop_environment}
+-------------------------+

VNC 密码: ${IFVNCPASSWD}

\033[0m
"""
}
Dscription(){
echo -e """
￴￴￴￴ ￴￴ ￴￴ ￴ ￴ ￴￴￴ ￴￴￴￴ ￴￴ ￴￴￴￴ ￴￴ ￴ ￴￴￴ ￴ ￴￴▃ ▆ █
𝙄𝙈𝙋𝙊𝙎𝙎𝙄𝘽𝙇𝙀  𝙄𝙎 𝙉𝙊𝙏𝙃𝙄𝙉𝙂
没   有   不   可   能
\033[0;32m
❌: 可能完全不适配 Ankali
❓: 可能适配 Ankali，但是效果不是很好
✅: 基本适配 Ankali，是建议安装的桌面
\033[1;33m
+----------------+-------------------------------------------------------+
| 桌面环境       | 描述和支持                                            |
+----------------+-------------------------------------------------------+
| GNOME      ❌  | 一个简洁、现代的桌面环境。但是Ankali可能不支持        |
| KDE Plasma ❓  | 功能丰富，高度可定制的桌面环境。Ankali可能勉强能用    |
| XFCE       ✅  | 轻量级桌面环境，注重性能和资源效率。Ankali已经完美适配|
| MATE       ✅  | GNOME 2的分支，提供传统桌面体验。                     |
| LXDE       ✅  | 极其轻量级的桌面环境，适合老旧或低配置的计算机。      |
| E17        ✅  | 高度的可定制性、现代的视觉效果和对资源的高效利用      |
| I3WM       ❌  | 有着其简洁、轻量级和对键盘快捷键的重视                |
+----------------+-------------------------------------------------------+

+------------+------------------+
|  桌面环境  | 约所需的占用空间 |
+------------+------------------+
| GNOME      |     2,800 MB     |
| KDE Plasma |     4,000 MB     |
| XFCE       |     2,300 MB     |
| MATE       |     2,600 MB     |
| LXDE       |     2,300 MB     |
| E17        |     1,900 MB     |
| I3WM       |     1,900 MB     |
+------------+------------------+
\033[0m
"""
}

IFVNCINSTALL(){
VNCINSTALLIF
if [ "$VNCINSTALL" = "true" ]; then
    VNCINSTALL="已安装"
elif [ "$VNCINSTALL" = "false" ]; then
    VNCINSTALL="未安装"
else
    VNCINSTALL="未知"
fi
}
IFNOVNCINSTALL(){
NOVNCINSTALLIF
if [ "$NOVNCINSTALL" = "true" ]; then
    NOVNCINSTALL="已安装"
elif [ "$NOVNCINSTALL" = "false" ]; then
    NOVNCINSTALL="未安装"
else
    NOVNCINSTALL="未知"
fi
}
source /.kali-config/language.conf
while true; do
    log "[INFO] 显示主菜单" "${CYAN}请选择一个选项:${RESET}"
    echo -e "${GREEN}[1]${RESET} ${BLUE}安装 GUI 图形桌面${RESET}"
    echo -e "${GREEN}[2]${RESET} ${BLUE}卸载 GUI 图形桌面${RESET}"
    echo -e "${GREEN}[3]${RESET} ${BLUE}VNC 服务${RESET}"
    echo -e "${GREEN}[4]${RESET} ${BLUE}退出${RESET}"
    read -p "请输入你的选择: " choice

    if [[ $choice -eq 1 ]]; then
        # GUI 图形桌面菜单
        while true; do
            Dscription
            log "[INFO] 显示 GUI 图形桌面菜单" "${CYAN}选择一个图形桌面环境安装:${RESET}"
            echo -e "${GREEN}[1]${RESET} GNOME"
            echo -e "${GREEN}[2]${RESET} KDE Plasma"
            echo -e "${GREEN}[3]${RESET} XFCE"
            echo -e "${GREEN}[4]${RESET} MATE"
            echo -e "${GREEN}[5]${RESET} LXDE"
            echo -e "${GREEN}[6]${RESET} E17 (Enlightenment)"
            echo -e "${GREEN}[7]${RESET} I3WM"
            echo -e "${GREEN}[0]${RESET} 退出"
            read -p "请输入你的选择: " gui_choice

            case $gui_choice in
                1)
                    log "[INFO] 安装 GNOME" "${YELLOW}[+] 正在安装 GNOME...${RESET}"
                    apt install -y kali-desktop-gnome
                    desktop_operate
                    desktop_time_repair
                    echo "kali-desktop-gnome" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                2)
                    log "[INFO] 安装 KDE Plasma" "${YELLOW}[+] 正在安装 KDE Plasma...${RESET}"
                    apt install -y kali-desktop-kde
                    desktop_operate
                    desktop_time_repair
                    echo "kali-desktop-kde" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                3)
                    log "[INFO] 安装 XFCE" "${YELLOW}[+] 正在安装 XFCE...${RESET}"
                    apt install -y kali-desktop-xfce
                    desktop_operate
                    desktop_time_repair
                    echo "kali-desktop-xfce" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                4)
                    log "[INFO] 安装 MATE" "${YELLOW}[+] 正在安装 MATE...${RESET}"
                    apt install -y kali-desktop-mate
                    desktop_operate
                    desktop_time_repair
                    echo "kali-desktop-mate" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                5)
                    log "[INFO] 安装 LXDE" "${YELLOW}[+] 正在安装 LXDE...${RESET}"
                    apt install -y kali-desktop-lxde
                    desktop_operate
                    desktop_time_repair
                    echo "kali-desktop-lxde" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                6)
                    log "[INFO] 安装 E17" "${YELLOW}[+] 正在安装 E17...${RESET}"
                    apt install -y kali-desktop-e17
                    desktop_operate
                    desktop_time_repair
                    echo "kali-desktop-e17" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                7)
                    log "[INFO] 安装 i3wm" "${YELLOW}[+] 正在安装 i3wm...${RESET}"
                    apt install -y kali-desktop-i3
                    desktop_operate
                    desktop_time_repair
                    echo "kali-desktop-i3" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                0)
                    exit 1
                    ;;
                *)
                    log "[WARNING] 无效选择" "${RED}[!] 无效的选择，请重试。${RESET}"
                    ;;
            esac
        done
        break
    elif [[ $choice -eq 2 ]]; then
    /.kali-config/linux/undesktop
    break
    elif [[ $choice -eq 3 ]]; then
        # VNC 服务菜单
        while true; do
            IFVNCINSTALL
            IFNOVNCINSTALL
            vnc-installation
            log "[INFO] 显示 VNC 服务菜单" "${CYAN}选择一个VNC服务选项:${RESET}"
            echo -e "${GREEN}[1]${RESET} 安装 VNC 服务: $VNCINSTALL"
            echo -e "${GREEN}[2]${RESET} 安装 NOVNC 服务: $NOVNCINSTALL"
            echo -e "${GREEN}[3]${RESET} 配置 VNC 密码"
            echo -e "${GREEN}[4]${RESET} 直接启动桌面与 VNC 服务"
            echo -e "${GREEN}[5]${RESET} 启动 VNC + NOVNC 服务"
            echo -e "${GREEN}[0]${RESET} 退出"
            read -p "请输入你的选择: " vnc_choice

            case $vnc_choice in
                1)
                    log "[INFO] 安装 VNC 服务" "${YELLOW}[+] 正在安装VNC服务...${RESET}"
                    apt install -y tigervnc-standalone-server xinit
                    IFVNCINSTALL
                    ;;
                2)
                    sudo apt update && sudo apt upgrade -y
                    sudo apt install novnc websockify -y
                    IFNOVNCINSTALL
                    ;;
                3)
                    log "[INFO] 配置 VNC 密码" "${YELLOW}[+] 正在配置VNC密码...${RESET}"
                    vncpasswd
                    ;;
                4)
                    if [ "$VNCINSTALL" = "未安装" ]; then
                    log "[INFO]你还未安装 VNC 服务，你可以安装他" "${RED}[!] 你还未安装 VNC 服务，你可以安装它"
                    else
                    if [ -e ~/.vnc/passwd ]; then
                    log "The password has been set correctly. Continue to run the desktop."
                    else
                    log "[INFO] 开始配置 VNC 密码" "${YELLOW}[+] 您还未配置 VNC 密码，您需要配置 VNC 密码...${RESET}"
                    vncpasswd
                    fi
                    # 检查已安装的桌面环境
                    if [ -f ~/.kali-config/kali-desktop ]; then
                        desktops=($(cat ~/.kali-config/kali-desktop))
                        if [ ${#desktops[@]} -gt 1 ]; then
                            log "[INFO] 找到多个桌面环境" "${CYAN}找到多个桌面，请选择一个开始${RESET}"
                            for i in "${!desktops[@]}"; do
                                echo -e "${GREEN}[$((i+1))]${RESET} ${desktops[$i]}"
                            done
                            read -p "请输入你的选择: " desktop_choice
                            selected_desktop=${desktops[$((desktop_choice-1))]}
                        else
                            selected_desktop=${desktops[0]}
                        fi
                        log "[INFO] 启动 VNC 服务" "${YELLOW}[+] 正在使用 ${selected_desktop} 启动 VNC 服务...${RESET}"

                        # 启动选定的桌面环境
                        case $selected_desktop in
                            "kali-desktop-gnome")
                                startx &
                                ;;
                            "kali-desktop-kde")
                                startplasma-x11 &
                                ;;
                            "kali-desktop-xfce")
                                startxfce4 &
                                ;;
                            "kali-desktop-mate")
                                mate-session &
                                ;;
                            "kali-desktop-lxde")
                                startlxde &
                                ;;
                            "kali-desktop-e17")
                                enlightenment_start &
                                ;;
                            "kali-desktop-i3")
                                i3 &
                                ;;
                            *)
                                log "[ERROR] 未识别的桌面环境" "${RED}[!] 未识别的桌面环境，尝试手动启动。${RESET}"
                                ;;
                        esac

                        # 启动VNC服务器并获取输出
                        output=$(vncserver 2>&1)
                        echo "VNC server output:"
                        echo "$output"

                        # 提取显示编号 (例如 :1)
                        display_number=$(echo "$output" | grep -oP "(?<=:)[0-9]+" | head -n 1)

                        # 检查是否成功获取显示编号并计算端口
                        if [ -n "$display_number" ]; then
                            vnc_port=$((5900 + display_number))
                            echo "VNC 服务器已运行至: $vnc_port"
                            log "[SUCCESS] 成功启动 VNC 服务器" "${GREEN}[+] VNC 服务器成功启动在端口: $vnc_port${RESET}"
                            echo -e "${YELLOW}你可以直接使用以下地址连接VNC服务器:"
                            echo "127.0.0.1:$vnc_port"
                            echo "localhost:$vnc_port"
                            echo "0.0.0.0:$vnc_port"
                            echo
                            echo -e "${YELLOW}你还可以尝试以下地址去连接你的VNC服务器:"
                            ip_addresses=$(hostname -I)
                            for ip in $ip_addresses; do
                            echo "$ip:$vnc_port"
                            done
                        else
                            echo "无法确定VNC服务器端口号，请自行查看日志信息"
                            log "[ERROR] 启动 VNC 服务器失败" "${RED}[!] 启动VNC服务器失败。检查问题...${RESET}"

                            # 检查Xorg日志文件中的错误
                            if grep -q "no screens found" /var/log/Xorg.0.log; then
                                log "[ERROR] 未找到屏幕" "${RED}[!] 未找到屏幕。您可能需要安装或配置虚拟显示器如Xvfb。${RESET}"
                            fi
                            log "[INFO] 检查日志文件以获取更多信息" "${YELLOW}[i] 您也可以在 /var/log/Xorg.0.log 中检查日志文件以获取更多详细信息。${RESET}"
                        fi

                    else
                        log "[ERROR] 未找到桌面环境" "${RED}[!] 未找到桌面环境，请先安装一个。${RESET}"
                    fi
                    fi
                    ;;
                5)
                    if [ "$NOVNCINSTALL" = "未安装" ]; then
                    log "[INFO]你还未安装 NOVNC 服务，你可以安装他" "${RED}[!] 你还未安装 NOVNC 服务，你可以安装它"
                    else
                    if [ "$VNCINSTALL" = "未安装" ]; then
                    log "[INFO]你还未安装 VNC 服务，你可以安装他" "${RED}[!] 你还未安装 VNC 服务，你可以安装它"
                    else
                    if [ -e ~/.vnc/passwd ]; then
                    log "The password has been set correctly. Continue to run the desktop."
                    else
                    log "[INFO] 开始配置 VNC 密码" "${YELLOW}[+] 您还未配置 VNC 密码，您需要配置 VNC 密码...${RESET}"
                    vncpasswd
                    fi
                    # 检查已安装的桌面环境
                    if [ -f ~/.kali-config/kali-desktop ]; then
                        desktops=($(cat ~/.kali-config/kali-desktop))
                        if [ ${#desktops[@]} -gt 1 ]; then
                            log "[INFO] Multiple desktop environments found" "${CYAN}找到多个桌面环境，请选择一个开始${RESET}"
                            for i in "${!desktops[@]}"; do
                                echo -e "${GREEN}[$((i+1))]${RESET} ${desktops[$i]}"
                            done
                            read -p "请输入你的选择: " desktop_choice
                            selected_desktop=${desktops[$((desktop_choice-1))]}
                        else
                            selected_desktop=${desktops[0]}
                        fi
                        log "[INFO] Starting VNC server" "${YELLOW}[+] 正在使用 ${selected_desktop} 启动 VNC 服务...${RESET}"

                        # 启动选定的桌面环境
                        case $selected_desktop in
                            "kali-desktop-gnome")
                                startx &
                                ;;
                            "kali-desktop-kde")
                                startplasma-x11 &
                                ;;
                            "kali-desktop-xfce")
                                startxfce4 &
                                ;;
                            "kali-desktop-mate")
                                mate-session &
                                ;;
                            "kali-desktop-lxde")
                                startlxde &
                                ;;
                            "kali-desktop-e17")
                                enlightenment_start &
                                ;;
                            "kali-desktop-i3")
                                i3 &
                                ;;
                            *)
                                log "[ERROR] Unrecognized desktop environment" "${RED}[!] 未识别的桌面环境，尝试手动启动。${RESET}"
                                ;;
                        esac

                        # 启动VNC服务器并获取输出
                        output=$(vncserver 2>&1)
                        echo "VNC server output:"
                        echo "$output"

                        # 提取显示编号 (例如 :1)
                        display_number=$(echo "$output" | grep -oP "(?<=:)[0-9]+" | head -n 1)

                        # 检查是否成功获取显示编号并计算端口
                        if [ -n "$display_number" ]; then
                            vnc_port=$((5900 + display_number))
                            echo "VNC 服务器已运行至: $vnc_port"
                            log "[SUCCESS] Successfully started VNC server" "${GREEN}[+] VNC 服务器成功启动在端口: $vnc_port${RESET}"

                            # 启动 noVNC 和 websockify
                            log "[INFO] Starting noVNC server" "${YELLOW}[+] 正在启动 noVNC 服务器...${RESET}"
                            websockify --web=/usr/share/novnc/ 6080 localhost:$vnc_port &
                            if [ $? -eq 0 ]; then
                                log "[SUCCESS] noVNC server started successfully" "${GREEN}[+] noVNC 服务器成功启动在端口 6080。${RESET}"
                                echo -e "${YELLOW}你可以使用以下 URL 连接到 noVNC 服务器:"
                                echo "http://localhost:6080/vnc.html"
                                echo "http://127.0.0.1:6080/vnc.html"
                            else
                                log "[ERROR] Failed to start noVNC server" "${RED}[!] 启动 noVNC 服务器失败。请检查配置和日志。${RESET}"
                            fi
                        else
                            echo "无法确定 VNC 服务器端口号，请自行查看日志信息"
                            log "[ERROR] Failed to start VNC server" "${RED}[!] 启动 VNC 服务器失败。检查问题...${RESET}"

                            # 检查 Xorg 日志文件中的错误
                            if grep -q "no screens found" /var/log/Xorg.0.log; then
                                log "[ERROR] No screens found" "${RED}[!] 未找到屏幕。您可能需要安装或配置虚拟显示器如 Xvfb。${RESET}"
                            fi
                            log "[INFO] Check log files for more information" "${YELLOW}[i] 您也可以在 /var/log/Xorg.0.log 中检查日志文件以获取更多详细信息。${RESET}"
                        fi

                    else
                        log "[ERROR] No desktop environments found" "${RED}[!] 未找到桌面环境，请先安装一个。${RESET}"
                    fi
                    fi
                    fi
                    ;;

                0)
                    exit 1
                    ;;
                *)
                    log "[WARNING] 无效选择" "${RED}[!] 无效的选择，请重试。${RESET}"
                    ;;
            esac
        done
        break
        elif [[ $choice -eq 4 ]]; then
        break
        exit 1
    else
        log "[WARNING] 无效选择" "${RED}[!] 无效的选择，请重试。${RESET}"
    fi
done
}

# English menu
menu_en(){
vnc-installation(){
if [ -e ~/.vnc/passwd ]; then
IFVNCPASSWD="Is Set"
else
IFVNCPASSWD="Not set"
fi
if [ -e ~/.kali-config/kali-desktop ]; then
Desktop_environment=$(cat ~/.kali-config/kali-desktop)
else
Desktop_environment="No desktop environment yet."
fi
echo -e """
\033[1;33m
+-----------------+----------------+
    VNC Service     Is installed?   
+-----------------+----------------+
 VNC server          $VNCINSTALL   
 NOVNC               $NOVNCINSTALL  
+-----------------+----------------+

+-----------------------------+
Installed desktop environment.       
+-----------------------------+
${Desktop_environment}
+-----------------------------+

VNC Password: ${IFVNCPASSWD}

\033[0m
"""
}

Dscription(){
echo -e """
￴￴￴￴ ￴￴ ￴￴ ￴ ￴ ￴￴￴ ￴￴￴￴ ￴￴ ￴￴￴￴ ￴￴ ￴ ￴￴￴ ￴ ￴￴▃ ▆ █
𝙄𝙈𝙋𝙊𝙎𝙎𝙄𝘽𝙇𝙀  𝙄𝙎 𝙉𝙊𝙏𝙃𝙄𝙉𝙂
没有不可能
\033[0;32m
❌: Not compatible with Ankali
❓: Partially compatible with Ankali
✅: Fully compatible with Ankali
\033[1;33m
+----------------+-----------------------------------------------------+
| Desktop        | Description and Support                             |
+----------------+-----------------------------------------------------+
| GNOME      ❌  | A clean, modern desktop environment.                |
| KDE Plasma ❓  | Feature-rich, highly customizable.                  |
| XFCE       ✅  | Lightweight, performance-focused. Fully supported.  |
| MATE       ✅  | A fork of GNOME 2, offers a traditional experience. |
| LXDE       ✅  | Very lightweight, ideal for low-end hardware.       |
| E17        ✅  | Highly customizable, modern visuals                 |
| I3WM       ❌  | Simple, lightweight, keyboard-driven.               |
+----------------+-----------------------------------------------------+

+------------+----------------+
|  Desktop   | Disk Space (MB)|
+------------+----------------+
| GNOME      |     2,800      |
| KDE Plasma |     4,000      |
| XFCE       |     2,300      |
| MATE       |     2,600      |
| LXDE       |     2,300      |
| E17        |     1,900      |
| I3WM       |     1,900      |
+------------+----------------+
\033[0m
"""
}

IFVNCINSTALL(){
VNCINSTALLIF
if [ "$VNCINSTALL" = "true" ]; then
    VNCINSTALL="Installed"
elif [ "$VNCINSTALL" = "false" ]; then
    VNCINSTALL="Not installed"
else
    VNCINSTALL="unknown"
fi
}
IFNOVNCINSTALL(){
NOVNCINSTALLIF
if [ "$NOVNCINSTALL" = "true" ]; then
    NOVNCINSTALL="Installed"
elif [ "$NOVNCINSTALL" = "false" ]; then
    NOVNCINSTALL="Not installed"
else
    NOVNCINSTALL="unknown"
fi
}

source /.kali-config/language.conf
while true; do
    log "[i] Displaying main menu" "${CYAN}Please choose an option:${RESET}"
    echo -e "${GREEN}[1]${RESET} ${BLUE}Install GUI Graphical Desktop${RESET}"
    echo -e "${GREEN}[2]${RESET} ${BLUE}uninstall GUI Graphical Desktop${RESET}"
    echo -e "${GREEN}[3]${RESET} ${BLUE}VNC Service${RESET}"
    echo -e "${GREEN}[4]${RESET} ${BLUE}Exit${RESET}"
    read -p "Enter your choice: " choice

    if [[ $choice -eq 1 ]]; then
        # GUI Graphical Desktop Menu
        while true; do
            Dscription
            log "[i] User selected option 1: Install GUI Graphical Desktop" "${CYAN}Choose a graphical desktop environment to install:${RESET}"
            echo -e "${GREEN}[1]${RESET} GNOME"
            echo -e "${GREEN}[2]${RESET} KDE Plasma"
            echo -e "${GREEN}[3]${RESET} XFCE"
            echo -e "${GREEN}[4]${RESET} MATE"
            echo -e "${GREEN}[5]${RESET} LXDE"
            echo -e "${GREEN}[6]${RESET} E17 (Enlightenment)"
            echo -e "${GREEN}[7]${RESET} I3WM"
            echo -e "${GREEN}[0]${RESET} Exit"
            read -p "Enter your choice: " gui_choice

            case $gui_choice in
                1)
                    log "[i] Installing GNOME..." "${YELLOW}[+] Installing GNOME...${RESET}"
                    apt install -y kali-desktop-gnome
                    desktop_operate
                    echo "kali-desktop-gnome" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                2)
                    log "[i] Installing KDE Plasma..." "${YELLOW}[+] Installing KDE Plasma...${RESET}"
                    apt install -y kali-desktop-kde
                    desktop_operate
                    echo "kali-desktop-kde" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                3)
                    log "[i] Installing XFCE..." "${YELLOW}[+] Installing XFCE...${RESET}"
                    apt install -y kali-desktop-xfce
                    desktop_operate
                    echo "kali-desktop-xfce" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                4)
                    log "[i] Installing MATE..." "${YELLOW}[+] Installing MATE...${RESET}"
                    apt install -y kali-desktop-mate
                    desktop_operate
                    echo "kali-desktop-mate" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                5)
                    log "[i] Installing LXDE..." "${YELLOW}[+] Installing LXDE...${RESET}"
                    apt install -y kali-desktop-lxde
                    desktop_operate
                    echo "kali-desktop-lxde" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                
                6)
                    log "[INFO] Installing E17" "${YELLOW}[+] Installing E17...${RESET}"
                    apt install -y kali-desktop-e17
                    desktop_operate
                    desktop_time_repair
                    echo "kali-desktop-e17" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                7)
                    log "[INFO] Installing i3wm" "${YELLOW}[+] Installing i3wm...${RESET}"
                    apt install -y kali-desktop-i3
                    desktop_operate
                    desktop_time_repair
                    echo "kali-desktop-i3" >> ~/.kali-config/kali-desktop
                    break
                    ;;
                0)
                    log "[i] User chose to exit from GUI installation menu" "Exiting..."
                    exit 1
                    ;;
                *)
                    log "[!] Invalid choice in GUI menu" "${RED}[!] Invalid choice, please try again.${RESET}"
                    ;;
            esac
        done
        break
    elif [[ $choice -eq 2 ]]; then
    /.kali-config/linux/undesktop
    break
    elif [[ $choice -eq 3 ]]; then
        IFVNCINSTALL
        IFNOVNCINSTALL
        # VNC Service Menu
        while true; do
            vnc-installation
            log "[i] User selected option 2: VNC Service" "${CYAN}Choose a VNC service option:${RESET}"
            echo -e "${GREEN}[1]${RESET} Install VNC Service: $VNCINSTALL"
            echo -e "${GREEN}[2]${RESET} Install NOVNC Service: $NOVNCINSTALL"
            echo -e "${GREEN}[3]${RESET} Configure VNC Password"
            echo -e "${GREEN}[4]${RESET} Start the graphical desktop and VNC service directly"
            echo -e "${GREEN}[5]${RESET} Start VNC + NOVNC services"
            echo -e "${GREEN}[0]${RESET} Exit"
            read -p "Enter your choice: " vnc_choice

            case $vnc_choice in
                1)
                    log "[i] Installing VNC Service..." "${YELLOW}[+] Installing VNC Service...${RESET}"
                    apt install -y tigervnc-standalone-server xinit
                    IFVNCINSTALL
                    ;;
                2)
                    sudo apt update && sudo apt upgrade -y
                    sudo apt install novnc websockify -y
                    IFNOVNCINSTALL
                    ;;
                3)
                    log "[i] Configuring VNC Password..." "${YELLOW}[+] Configuring VNC Password...${RESET}"
                    vncpasswd
                    ;;
                4)
                    if [ "$VNCINSTALL" = "Not installed" ]; then
                    log "[INFO]You have not installed the VNC service yet, you can install it." "${RED}[!] You have not installed the VNC service yet, you can install it."
                    else
                    if [ -e ~/.vnc/passwd ]; then
                    log "The password has been set correctly. Continue to run the desktop."
                    else
                    log "[INFO] Start configuring VNC password" "${YELLOW}[+] You have not configured the VNC password, you need to configure the VNC password....${RESET}"
                    vncpasswd
                    fi
                    # Check installed desktop environments
                    if [ -f ~/.kali-config/kali-desktop ]; then
                        desktops=($(cat ~/.kali-config/kali-desktop))
                        if [ ${#desktops[@]} -gt 1 ]; then
                            log "[INFO] Multiple desktop environments found" "${CYAN}Multiple desktops found, please choose one to start${RESET}"
                            for i in "${!desktops[@]}"; do
                                echo -e "${GREEN}[$((i+1))]${RESET} ${desktops[$i]}"
                            done
                            read -p "Please enter your choice: " desktop_choice
                            selected_desktop=${desktops[$((desktop_choice-1))]}
                        else
                            selected_desktop=${desktops[0]}
                        fi
                        log "[INFO] Starting VNC server" "${YELLOW}[+] Starting VNC server with ${selected_desktop}...${RESET}"

                        # Start the selected desktop environment
                        case $selected_desktop in
                            "kali-desktop-gnome")
                                startx &
                                ;;
                            "kali-desktop-kde")
                                startplasma-x11 &
                                ;;
                            "kali-desktop-xfce")
                                startxfce4 &
                                ;;
                            "kali-desktop-mate")
                                mate-session &
                                ;;
                            "kali-desktop-lxde")
                                startlxde &
                                ;;
                            "kali-desktop-e17")
                                enlightenment_start &
                                ;;
                            "kali-desktop-i3")
                                i3 &
                                ;;
                            *)
                                log "[ERROR] Unrecognized desktop environment" "${RED}[!] Unrecognized desktop environment, trying to start manually.${RESET}"
                                ;;
                        esac

                        # Start VNC server and capture output
                        output=$(vncserver 2>&1)
                        echo "VNC server output:"
                        echo "$output"

                        # Extract display number (e.g., :1)
                        display_number=$(echo "$output" | grep -oP "(?<=:)[0-9]+" | head -n 1)

                        # Check if display number was successfully retrieved and calculate the port
                        if [ -n "$display_number" ]; then
                            vnc_port=$((5900 + display_number))
                            echo "VNC server is running on: $vnc_port"
                            log "[SUCCESS] Successfully started VNC server" "${GREEN}[+] VNC server successfully started on port: $vnc_port${RESET}"
                            echo -e "${YELLOW}You can directly use the following addresses to connect to the VNC server:"
                            echo "127.0.0.1:$vnc_port"
                            echo "localhost:$vnc_port"
                            echo "0.0.0.0:$vnc_port"
                            echo
                            echo -e "${YELLOW}You can also try the following addresses to connect to your VNC server:"
                            ip_addresses=$(hostname -I)
                            for ip in $ip_addresses; do
                            echo "$ip:$vnc_port"
                            done
                        else
                            echo "Unable to determine the VNC server port, please check the log information."
                            log "[ERROR] Failed to start VNC server" "${RED}[!] Failed to start VNC server. Checking for issues...${RESET}"

                            # Check for errors in Xorg log files
                            if grep -q "no screens found" /var/log/Xorg.0.log; then
                                log "[ERROR] No screens found" "${RED}[!] No screens found. You may need to install or configure a virtual display like Xvfb.${RESET}"
                            fi
                            log "[INFO] Check log files for more information" "${YELLOW}[i] You can also check the log files in /var/log/Xorg.0.log for more details.${RESET}"
                        fi

                    else
                        log "[ERROR] No desktop environments found" "${RED}[!] No desktop environments found, please install one first.${RESET}"
                    fi
                    fi
                    ;;
                5)
                    if [ "$NOVNCINSTALL" = "Not installed" ]; then
                    log "[INFO]You have not installed the NOVNC service yet, you can install it." "${RED}[!] You have not installed the NOVNC service yet, you can install it."
                    else
                    if [ "$VNCINSTALL" = "Not installed" ]; then
                    log "[INFO]You have not installed the VNC service yet, you can install it." "${RED}[!] You have not installed the VNC service yet, you can install it."
                    else
                    if [ -e ~/.vnc/passwd ]; then
                    log "The password has been set correctly. Continue to run the desktop."
                    else
                    log "[INFO] Start configuring VNC password" "${YELLOW}[+] You have not configured the VNC password, you need to configure the VNC password....${RESET}"
                    vncpasswd
                    fi
                    # Check installed desktop environments
                    if [ -f ~/.kali-config/kali-desktop ]; then
                        desktops=($(cat ~/.kali-config/kali-desktop))
                        if [ ${#desktops[@]} -gt 1 ]; then
                            log "[INFO] Multiple desktop environments found" "${CYAN}Multiple desktop environments found, please choose one to start.${RESET}"
                            for i in "${!desktops[@]}"; do
                                echo -e "${GREEN}[$((i+1))]${RESET} ${desktops[$i]}"
                            done
                            read -p "Enter your choice: " desktop_choice
                            selected_desktop=${desktops[$((desktop_choice-1))]}
                        else
                            selected_desktop=${desktops[0]}
                        fi
                        log "[INFO] Starting VNC server" "${YELLOW}[+] Starting VNC server using ${selected_desktop}...${RESET}"

                        # Start the selected desktop environment
                        case $selected_desktop in
                            "kali-desktop-gnome")
                                startx &
                                ;;
                            "kali-desktop-kde")
                                startplasma-x11 &
                                ;;
                            "kali-desktop-xfce")
                                startxfce4 &
                                ;;
                            "kali-desktop-mate")
                                mate-session &
                                ;;
                            "kali-desktop-lxde")
                                startlxde &
                                ;;
                            "kali-desktop-e17")
                                enlightenment_start &
                                ;;
                            "kali-desktop-i3")
                                i3 &
                                ;;
                            *)
                                log "[ERROR] Unrecognized desktop environment" "${RED}[!] Unrecognized desktop environment, attempting manual start.${RESET}"
                                ;;
                        esac

                        # Start VNC server and capture the output
                        output=$(vncserver 2>&1)
                        echo "VNC server output:"
                        echo "$output"

                        # Extract display number (e.g., :1)
                        display_number=$(echo "$output" | grep -oP "(?<=:)[0-9]+" | head -n 1)

                        # Check if display number was successfully obtained and calculate port
                        if [ -n "$display_number" ]; then
                            vnc_port=$((5900 + display_number))
                            echo "VNC server is running on port: $vnc_port"
                            log "[SUCCESS] Successfully started VNC server" "${GREEN}[+] VNC server successfully started on port: $vnc_port${RESET}"

                            # Start noVNC and websockify
                            log "[INFO] Starting noVNC server" "${YELLOW}[+] Starting noVNC server...${RESET}"
                            websockify --web=/usr/share/novnc/ 6080 localhost:$vnc_port &
                            if [ $? -eq 0 ]; then
                                log "[SUCCESS] noVNC server started successfully" "${GREEN}[+] noVNC server successfully started on port 6080.${RESET}"
                                echo -e "${YELLOW}You can connect to the noVNC server using the following URLs:"
                                echo "http://localhost:6080/vnc.html"
                                echo "http://127.0.0.1:6080/vnc.html"
                            else
                                log "[ERROR] Failed to start noVNC server" "${RED}[!] Failed to start noVNC server. Please check configuration and logs.${RESET}"
                            fi
                        else
                            echo "Unable to determine VNC server port, please check the log information."
                            log "[ERROR] Failed to start VNC server" "${RED}[!] Failed to start VNC server. Checking for issues...${RESET}"

                            # Check for errors in Xorg log file
                            if grep -q "no screens found" /var/log/Xorg.0.log; then
                                log "[ERROR] No screens found" "${RED}[!] No screens found. You may need to install or configure a virtual display like Xvfb.${RESET}"
                            fi
                            log "[INFO] Check log files for more information" "${YELLOW}[i] You can also check the log file in /var/log/Xorg.0.log for more details.${RESET}"
                        fi

                    else
                        log "[ERROR] No desktop environments found" "${RED}[!] No desktop environments found, please install one first.${RESET}"
                    fi
                    fi
                    fi
                    ;;
                0)
                    log "[i] User chose to exit from VNC Service menu" "Exiting..."
                    exit 1
                    ;;
                *)
                    log "[!] Invalid choice in VNC Service menu" "${RED}[!] Invalid choice, please try again.${RESET}"
                    ;;
            esac
        done
        break
        elif [[ $choice -eq 4 ]]; then
        break
        exit 1
    else
        log "[!] Invalid choice in main menu" "${RED}[!] Invalid choice, please try again.${RESET}"
    fi
done
}

# Judging Chinese or English
CONFIG_FILE="/.kali-config/Ankali.conf"
LANGUAGE=$(grep -v '^//' "$CONFIG_FILE" | grep -w 'LANGUAGE' | cut -d '=' -f2)
if [ "$LANGUAGE" = "CN" ]; then
    menu_cn
elif [ "$LANGUAGE" = "EN" ]; then
    menu_en
else
    menu_en
fi

# © Sansjtw Ankali
